package com.crud.web; /**
 * @author wzm
 * @create 2023-05-08 15:27
 */

import com.crud.bean.Employee;
import com.crud.service.EmployeeService;
import com.crud.service.impl.EmployeeServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/deleteById")
public class DeleteServlet extends HttpServlet {
    private EmployeeService employeeService = new EmployeeServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String id = request.getParameter("id");

        Employee employee = new Employee();
        employee.setId(Integer.parseInt(id));

        boolean remove = employeeService.remove(employee);
        if (remove){
            //成功
            response.sendRedirect(request.getContextPath()+"/findAll");
        }else{
            //失败
            response.getWriter().write("内部错误");
        }

    }
}
